<?php
namespace app\components\Pivot;

use yii\base\Widget;
use yii\helpers\Html;

class PivotListView extends Widget
{
    public $dataProvider;

    public function init(){
        parent::init();
        if ($this->dataProvider === null) {
            $this->dataProvider  = '';
        }else{
             $this->dataProvider = $this->dataProvider;

        }
    }

    public function run(){

    	$view = $this->getView();

        $items      = $this->dataProvider[0];
        $location   = $this->dataProvider[1];

        $outputData = '';
        
        $outputData .= '[';
        foreach ($items as  $item ) {

            $outputData .= '{';
            foreach ($item as $key => $value) {
                $outputData .= $key.': "'.$value.'",';
            }
            $outputData .= '},';

        }   
        $outputData .= ']';

        $outputHeader = '';
        
        $outputHeader .= '{';

            foreach ($location as $key => $items ) {
                
                $outputHeader .= $key.': [';
                    foreach ($items as $value ) {
                        $outputHeader .= '"'.$value.'",';
                    }
                $outputHeader .= '],';
                
            }    
        $outputHeader .= '}';



        $tag = "div";

        $html = '<div id="output" style="margin: 10px;"></div>';

        echo Html::tag( $tag, $html );
        
        //$this->registerClientScript();

        $query = '
            jQuery(function(){
                    jQuery("#output").pivot('.$outputData.','. $outputHeader.');
            });';

        $view->registerJs( $query );

    }   

    public function registerClientScript(){
        $view = $this->getView();
        PivotListViewAsset::register($view);
    }
}
?>